<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="类型首字母" prop="fctPy">
        <el-input
          v-model="queryParams.fctPy"
          placeholder="请输入类型首字母"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="fctname">
        <el-input
          v-model="queryParams.fctname"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进出口类型" prop="seriesplace">
        <el-input
          v-model="queryParams.seriesplace"
          placeholder="请输入进出口类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brandName">
        <el-select
          ref="brandid"
          v-model="queryParams.brandid"
          filterable
          clearable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in carBrandList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="fctId" prop="fctId">
        <el-input
          v-model="queryParams.fctId"
          placeholder="请输入fctId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:carFct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:carFct:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:carFct:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:carFct:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"  :pageName="$options.name" ></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="carFctList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id"  v-if="columns[0].visible" show-overflow-tooltip />
      <el-table-column label="类型首字母" align="center" prop="fctPy" v-if="columns[1].visible" show-overflow-tooltip />
      <el-table-column label="名称" align="center" prop="fctname" v-if="columns[2].visible" show-overflow-tooltip />
      <el-table-column label="进出口类型" align="center" prop="seriesplace" v-if="columns[3].visible" show-overflow-tooltip />
      <el-table-column label="品牌" align="center" prop="brandName" v-if="columns[4].visible" show-overflow-tooltip />
<!--      <el-table-column label="fctId" align="center" prop="fctId" v-if="columns[5].visible" show-overflow-tooltip />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:carFct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:carFct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改品牌车系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="类型首字母" prop="fctPy">
          <el-input v-model="form.fctPy" readonly placeholder="请输入类型首字母" />
        </el-form-item>
        <el-form-item label="名称" prop="fctname">
          <el-input v-model="form.fctname" placeholder="请输入名称"  @input="setFctPY" />
        </el-form-item>
        <el-form-item label="进出口类型" prop="seriesplace">
          <el-select v-model="form.seriesplace"  placeholder="请输入进出口类型">
            <el-option
              v-for="item in seriesPlaces"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brandid">
<!--          <el-input v-model="form.brandid" placeholder="请输入brandid" />-->
          <el-select
            ref="brandid"
            v-model="form.brandid"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in carBrandList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarFct, getCarFct, delCarFct, addCarFct, updateCarFct } from "@/api/system/carFct";
import { listCarBrand, getCarBrand, delCarBrand, addCarBrand, updateCarBrand } from "@/api/system/carBrand";

import pinyin from 'js-pinyin'
export default {
  name: "CarFct",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 品牌车系表格数据
      carFctList: [],
      carBrandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      seriesPlaces: [
        {
          value: '合资',
          label: '合资'
        },  {
          value: '进口',
          label: '进口'
        },  {
          value: '自主',
          label: '自主'
        },    {
          value: '独资',
          label: '独资'
        }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fctPy: null,
        fctname: null,
        seriesplace: null,
        brandid: null,
        fctId: null
      },
      // 表单参数
      form: {
        fctId: 100
      },
      // 表单校验
      rules: {
      },
      // 列表的列的显示隐藏设置
      columns:[
                  { key: 0, label: `id`, visible: true },
                  { key: 1, label: `类型首字母`, visible: true },
                  { key: 2, label: `名称`, visible: true },
                  { key: 3, label: `进出口类型`, visible: true },
                  { key: 4, label: `brandid`, visible: true },
                  { key: 5, label: `$column.columnComment`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
    //列表分页列的动态显示配置
    var columns = JSON.parse(localStorage.getItem(this.$options.name));
    if(columns){
      this.columns = columns;
    }
  },
  methods: {
    /** 查询品牌车系列表 */
    getList() {
      this.loading = true;
      listCarFct(this.queryParams).then(response => {
        this.carFctList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取品牌列表
    getCarBrands(query) {
      listCarBrand(query).then(response => {
        for(let item of response.rows) {
          item.label = item.name;
          item.value = item.id;
         // console.log(item);
        }
        this.carBrandList = response.rows;
        // console.log(this.carBrandList);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fctPy: null,
        fctname: null,
        seriesplace: null,
        brandid: null,
        fctId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //远程搜索品牌数据
    remoteMethod(query) {
      let params = {
         name: query
      }
      console.log(params);
      if (query !== '') {
        this.loading = true;
        this.getCarBrands(params);
      } else {
        this.carBrandList = [];
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品牌车系";
    },
    setFctPY(value){
        this.form.fctPy = this.getStrFirstPY(value);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.carBrandList.push({
        label : row.brandName,
        value :  row.brandid,
      })
      const id = row.id || this.ids
      getCarFct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品牌车系";
      });
    },
    getStrFirstPY(str) {
      pinyin.setOptions({checkPolyphone:false,charCase:0});
      return  pinyin.getCamelChars(str); // 返回拼音首字母
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCarFct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.fctId = 100
            addCarFct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除品牌车系编号为"' + ids + '"的数据项？').then(function() {
        return delCarFct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/carFct/export', {
        ...this.queryParams
      }, `carFct_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
